/* 
 * File:   Beamformer.cpp
 * Author: pawtom
 * 
 * Created on 25 styczeń 2013, 12:45
 */

#include "Beamformer.h"
#include "Echo.h"
#include <numeric>
#include <math.h>
#include <iostream>
#include <complex>

using namespace std;
 
Beamformer::Beamformer(vector<complex<double> > weights) {
    this->weights = weights;
}

Beamformer::Beamformer(int beamAngle, int rowsCount, int rowSpacing, int lambda) {
    this->weights = vector<complex<double> >(rowsCount);
    for(int rowNumber = 0; rowNumber < rowsCount; rowNumber++) {
        double imag = sin(2*M_PI*rowSpacing/lambda*sin(beamAngle)*rowNumber);   //todo poprawic to bo na bank (?) jest zle
        this->weights.at(rowNumber) = complex<double>(0, imag);
    }
}

Beamformer::Beamformer(const Beamformer& orig) {}

Beamformer::~Beamformer() {}

vector<complex<double> > Beamformer::formUsingFFT(Echo echo, int resolution) {
    vector<complex<double> > results(echo.getSamplesCount());
    if(echo.getRowsCount() != weights.size()) {
        cout << "dupa!" << endl;
        return results;
    }
    for(int sampleNumber = 0; sampleNumber < echo.getSamplesCount(); sampleNumber++) {
        vector<complex<double> > sample = echo.getSampleVector(sampleNumber);
        sample.resize(resolution, 0);
//        TODO doimplementować fft jak załapie co dokładnie tu ma byc...
    }
    
    return vector<complex<double> >(3);
}

vector<complex<double> > Beamformer::formUsingDFT(Echo echo) {  //TODO sprawdzic czy jest ok!!!!!!!
    vector<complex<double> > results(echo.getSamplesCount());
    if(echo.getRowsCount() != weights.size()) {
        cout << "dupa!" << endl;
        return results;
    }
    for(int sampleNumber = 0; sampleNumber < echo.getSamplesCount(); sampleNumber++) {
        complex<double> dotProduct;
        vector<complex<double> > sample = echo.getSampleVector(sampleNumber); 
//        cout << "pracuję nad próbką #" << sampleNumber << endl;
        for(int rowNumber = 0; rowNumber < echo.getRowsCount(); rowNumber++) {
            dotProduct += weights.at(rowNumber)*sample.at(rowNumber);
//            cout << "\tliczę wiersz #" << rowNumber << " -> " << weights.at(rowNumber) << "*" << sample.at(rowNumber) << endl;
        }
        results.push_back(dotProduct); //TODO liczę dot product
    }
    return results;
}
